#!/bin/sh

PATTERN='oksvn(?=\.com)|(?<=svn\.)valu(?=\.cn)|svnchina(?=\.com)|(?<=hx-system\.)googlecode(?=\.com)|unfuddle(?=\.com)'
SVN_CURRENT=`svn info 2<&- | grep -Po -m1 "$PATTERN"`
if [ 0 -ne $? ] || [ -z "$SVN_CURRENT" ]; then
	echo
	exit 1
fi

if [ 0 -eq $# ]; then
	tput setaf 6;tput bold;tput rev
	echo $SVN_CURRENT
	tput sgr0
	exit
fi

SVN_NEXT=$1
F_CUR=$SVN_CURRENT.tar
F_NEXT=$SVN_NEXT.tar
if [ "$SVN_CURRENT" = "$SVN_NEXT" ]; then
	printf 'This is '
	tput bold;tput setf 3;tput rev
	echo $SVN_NEXT
	tput sgr0
	exit 2
elif [ 'me' = "$SVN_NEXT" ]; then
	TAR_ARGS='--suffix=~ -c'
elif [ -n "$SVN_NEXT" ]; then
	if [ ! -f $F_NEXT ]; then
		tput bold;tput setf 4;tput rev
		printf 'Where is '
		echo "$F_NEXT?"
		tput sgr0
		exit 3
	fi
	TAR_ARGS='--remove-files -uv'
fi

#tar $TAR_ARGS -af $F_CUR `find . -type d -name '.svn'`\
tar $TAR_ARGS -af $F_CUR '.svn'\
	||exit 4
printf 'Package '
tput bold;tput setf 2;tput rev
printf $SVN_CURRENT
tput sgr0
echo ' successfully.'

if [ 'me' != "$SVN_NEXT" ] && [ -n "$SVN_NEXT" ]; then
	tar -axf $F_NEXT\
		||exit
	printf 'Success to '
	tput bold;tput setf 2;tput rev
	echo $SVN_NEXT
	tput sgr0
fi
